package com.xhwl.data.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.AccountDeleteLog;
import com.xhwl.common.query.cent.sdata.AccountDeleteLogQuery;
import com.xhwl.common.query.cent.sdata.OrganizationQuery;
import com.xhwl.data.pojo.dto.AccountDeleteLogQueryDTO;
import com.xhwl.data.pojo.dto.personal.AccountDeleteLogDTO;
import com.xhwl.data.service.IAccountDeleteLogService;
import com.xhwl.data.service.IOrganizationService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: luyi
 * @Description: 帐号删除记录， 离职人员记录
 * @Date: Create in 16:50 2022/4/1
 */
@RestController
public class AccountDeleteLogController {

    @Autowired
    private IAccountDeleteLogService accountDeleteLogService;

    @Autowired
    private IOrganizationService organizationService;

    @RequestMapping(value = "/account-delete-log/page")
    public ResultJson page(@RequestBody AccountDeleteLogQueryDTO query){
        Page<AccountDeleteLog> page = accountDeleteLogService.page(query.getAccountDeleteLogQuery(), query.getPage() == null ? new Page() : query.getPage());
        return ResultJson.success(convertDTO(page));
    }

    @RequestMapping(value = "/account-delete-log/delete")
    public ResultJson delete(@RequestBody AccountDeleteLogQuery accountDeleteLogQuery){
        if(null == accountDeleteLogQuery){
            return ResultJson.fail("参数错误");
        }
        QueryWrapper<AccountDeleteLog> accountDeleteLogQueryWrapper = new QueryWrapper<>();
        if(null != accountDeleteLogQuery.getEnterpriseId()){
            accountDeleteLogQueryWrapper.eq("enterprise_id", accountDeleteLogQuery.getEnterpriseId());
        }
        if(CollectionUtils.isNotEmpty(accountDeleteLogQuery.getIds())){
            accountDeleteLogQueryWrapper.in("id", accountDeleteLogQuery.getIds());
        }
        accountDeleteLogService.remove(accountDeleteLogQueryWrapper);
        return ResultJson.success();
    }

    private Page<AccountDeleteLogDTO> convertDTO(Page<AccountDeleteLog> page){
        Page<AccountDeleteLogDTO> result = new Page<>();
        result.setCurrent(page.getCurrent());
        result.setSize(page.getSize());
        result.setTotal(page.getTotal());
        List<AccountDeleteLog> records = page.getRecords();

        List<String> orgIds = records.stream().filter(i -> StringUtils.isNotEmpty(i.getOrgIds())).map(i -> i.getOrgIds()).collect(Collectors.toList());
        List<Integer> ids = new ArrayList<>();
        for (String orgId : orgIds) {
            String[] arr = orgId.split(",");
            List<Integer> intArr = Arrays.stream(arr).map(i -> Integer.valueOf(i)).collect(Collectors.toList());
            ids.addAll(intArr);
        }
        ids = ids.stream().distinct().collect(Collectors.toList());
        OrganizationQuery organizationQuery = new OrganizationQuery();
        organizationQuery.withIds(ids);
        Map<Integer, String> integerStringMap = organizationService.batchFullName(organizationQuery);

        if(CollectionUtils.isNotEmpty(records)){
            List<AccountDeleteLogDTO> list = new ArrayList<>();
            for (AccountDeleteLog record : records) {
                AccountDeleteLogDTO accountDeleteLogDTO = new AccountDeleteLogDTO();
                BeanUtils.copyProperties(record, accountDeleteLogDTO);

                if(StringUtils.isNotEmpty(record.getOrgIds())){
                    String orgIds1 = record.getOrgIds();

                    List<Integer> orgIds2 = Arrays.stream(orgIds1.split(",")).map(i -> Integer.valueOf(i)).collect(Collectors.toList());
                    List<String> collect = orgIds2.stream().map(i -> integerStringMap.get(i)).collect(Collectors.toList());
                    accountDeleteLogDTO.setOrgs(collect);
                }
                list.add(accountDeleteLogDTO);
            }
            result.setRecords(list);
        }
        return result;
    }
}

